﻿using System;
using Library.Common;
using Library.Common.Helper;
using Microsoft.Extensions.Logging;
namespace Library.Helper
{
    /// <summary>
    /// 企信通-新平台-通知类
    /// </summary>
    public class SzlhllNewNotice
    {
        private static readonly bool entitySmsOper = Appsettings.App(new string[] { "entitySmsOper", "switch" }).ObjToBool();
        private readonly string sms_api_url = Appsettings.App(new string[] { "entitySmsOper", "sms_api_url" });
        private readonly string sms_user_name = Appsettings.App(new string[] { "entitySmsOper", "sms_user_name" });
        private readonly string sms_user_pwd = Appsettings.App(new string[] { "entitySmsOper", "sms_user_pwd" });
        private readonly ILogger _log;
        public SzlhllNewNotice(ILogger<SzlhllNewNotice> logger)
        {
            _log = logger;
        }
        /// <summary>
        /// 获取剩余短信数量
        /// </summary>
        /// <param name="message">返回消息</param>
        /// <returns></returns>
        public int GetOverSmsNumber(out string message)
        {
            message = "查询出现错误！";

            if (entitySmsOper)
            {
                return -1;
            }
            try
            {
                string url = $"{sms_api_url}/sms.aspx?userid=&account={sms_user_name}&password={Md5Encrypt.Encrypt(sms_user_pwd).ToUpper()}&action=overage";
                //string result = HttpRequest("", 1, 0, url, 0, "", "获取剩余短信数量").return_content;
                string result = HttpHelp.HttpHelp.ApiGet(url);
                if (result.IndexOf("<returnstatus>Sucess") != -1)
                {
                    string[] return_msg = result.Split(new string[] { "<overage>" }, StringSplitOptions.None);
                    if (return_msg.Length > 1)
                    {
                        result = return_msg[1].Split(new string[] { "</overage>" }, StringSplitOptions.None)[0];
                        message = "success";
                        return result.ObjToInt();
                    }
                }
                message = "短信账号错误，查询失败！";
                return -1;
            }
            catch (Exception ee)
            {
                //添加错误日志
                _log.LogError(1,ee, "GetOverSmsNumber");
                return -1;
            }
        }

        /// <summary>
        /// 发送短信消息
        /// </summary>
        /// <param name="sendMobiles">发送手机号列表，按英文逗号分割：18000001,1800002（最多1000个）</param>
        /// <param name="content">短信发送内容</param>
        /// <param name="message">返回消息</param>
        /// <returns></returns>
        public bool Send(string sendMobiles, string content, out string message)
        {
            message = "发送失败！";
            if (!entitySmsOper)
            {
                message = "开关未开启!";
                return false;
            }
            #region 发送短信==================================
            try
            {
                string url = $"{sms_api_url}/sms.aspx?";
                string postData = $"userid=&account={sms_user_name}&password={Md5Encrypt.Encrypt(sms_user_pwd).ToUpper()}&mobile={sendMobiles}&content={content} 【儿童之家】&sendTime=&action=send";
                url += postData;
                //string result = new HttpSendLogApp().HttpRequest("", 1, 1, url, 0, postData, "").return_content;
                string result = HttpHelp.HttpHelp.ApiGet(url);
                if (result.IndexOf("<returnstatus>Success</returnstatus>") == -1)
                {
                    //发送失败
                    message = $"短信发送失败{sendMobiles.Split(',').Length}条！";
                    return false;
                }
                else
                {
                    //发送成功
                    message = $"短信发送成功{sendMobiles.Split(',').Length}条！";
                    return true;
                }
            }
            catch (Exception ee)
            {
                //添加错误日志
                _log.LogError(1,ee, "SmsSend");
                //发送失败
                message = $"短信发送失败{sendMobiles.Split(',').Length}条！";
                return false;
            }
            #endregion
        }
    }
}
